﻿using Simple.Tools.Verify;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace Simple.Tools.Entity.VerifyAttribute
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	/// <summary>
	/// 验证属性是否符合正则表达式
	/// </summary>
	[AttributeUsage(AttributeTargets.Property)]
	public class CurRegexAttribute : BaseVerifyAttribute
	{
		private int _min;
		private int _max;
		private string _regexMatch;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <param name="regexMatch"></param>
		public CurRegexAttribute(string name,string regexMatch) : base(name)
		{
			this._errorInfo = "填写错误,请填写正确格式的邮箱!";
			this._regexMatch = regexMatch;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="o"></param>
		/// <param name="prop"></param>
		/// <returns></returns>
		public override Tuple<string, bool> Verify(object o, PropertyInfo prop)
		{
			if (SimpleCheck.IsNull(o))
			{
				return Tuple.Create(GetErrorInfo(), false);
			}

			object value = prop.GetValue(o);

			if (SimpleCheck.IsNull(value))
			{
				return Tuple.Create(GetErrorInfo(), false);
			}
			bool result = false;
			if (!SimpleCheck.IsNullOrEmpty(this._regexMatch))
			{
				Regex regex = new Regex(this._regexMatch);
				Match m = regex.Match(value.ToString());
			   	result = m.Success;
			}
			if (result)
			{
				_errorInfo = "验证通过";
			}
			else
			{
				_errorInfo += " 数据校验失败!";
			}
			return Tuple.Create(GetErrorInfo(), result);
		}
	}
}
